package com.teragadgets.android.comic;

import java.io.IOException;
import java.util.Observable;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import com.teragadgets.android.comic.datamodel.WebComic;
import com.teragadgets.android.comic.datamodel.WebComicDatabase;
import com.teragadgets.android.comic.feed.WebComicFeedParser;

import android.content.Context;

public class WebComicManagerModel extends Observable {

	private Vector<WebComic> webComics;

	public WebComicManagerModel(Context ctx) {

		WebComicDatabase db = WebComicDatabase.open(ctx);
		try {
			webComics = new WebComicFeedParser(
					"http://andcomicsengine.appspot.com/index.jsp")
					.getWebComics();
			for (WebComic c : webComics) {
				c.setSelected(db.webComicExists(c.getID()));
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();

		this.setChanged();
		this.notifyObservers();
	}

	public Vector<WebComic> getWebComics() {
		return webComics;
	}
}
